package com.wjm.blog.core.http;

import com.wjm.blog.common.exception.BizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

@Component
public class RestTemplateService {

    @Autowired
    private RestTemplate restTemplate;


    @Resource(name = "facePlusRestTemplate")
    private RestTemplate facePlusRestTemplate;

    public String post(String url, String param) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> request = new HttpEntity<>(param, headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);
        return postForEntity.getBody();
    }

//    @Value("${X-Auth-Value}")
//    private String XAuthValue;
//    @Value("${X-Auth-AccessKey}")
//    private String XAuthAccessKey;
//    @Value("${X-Emp-No}")
//    private String XEmpNo;
//    @Value("${X-Tenant-Id}")
//    private String XTenantId;

    public String postNewLpn(String url, String param, HttpHeaders headers) {
//        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
//        headers.set("X-Auth-Value", XAuthValue);
//        headers.set("X-Auth-AccessKey", XAuthAccessKey);
//        headers.set("X-Emp-No", XEmpNo);
//        headers.set("X-Tenant-Id", XTenantId);
        HttpEntity<String> request = new HttpEntity<>(param, headers);

//        System.out.println("-----------------------------------------------");
//        System.out.println("postNewLpn:" + headers);
//        System.out.println("-----------------------------------------------");

        //此处加编码格式转换
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);
        return postForEntity.getBody();
    }


    public byte[] postEcssScanExport(String url, String param) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(param, headers);
        ResponseEntity<byte[]> responseEntity = restTemplate.postForEntity(url, request, byte[].class);
        return responseEntity.getBody();
    }

    public String postDocumentEcssScan(String url, String param) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> request = new HttpEntity<>(param, headers);

        //此处加编码格式转换
//        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
//        restTemplate.setErrorHandler(new AcceptResponseErrorHandler());
        ResponseEntity<String> postForEntity = facePlusRestTemplate.postForEntity(url, request, String.class);
        if (postForEntity.getStatusCode().equals(HttpStatus.BAD_REQUEST)) {
            throw new BizException(postForEntity.getBody());
        }

        return postForEntity.getBody();
    }


    //参数自行拼接，防止出现占位符
    public String get(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
//        headers.set(CommonEnum.Request.TOKEN, request.getHeader(CommonEnum.Request.TOKEN));
        HttpEntity<String> httpEntity = new HttpEntity<>(headers);
        return facePlusRestTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class).getBody();
    }

    public String getGongyglProtabs(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> httpEntity = new HttpEntity<>(headers);
        return facePlusRestTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class).getBody();
    }


    public static void main(String[] args) {

    }
}
